package device

import (
	"context"
	"device-admin/config/mysql"
	"device-admin/internal/dal"
	"device-admin/internal/dao"
	"device-admin/internal/models"

	"192.168.1.75/go-pkg/errorx"
)

// AddQuickReply 添加快速回复
func AddQuickReply(ctx context.Context, merchantID, text string) (string, error) {
	query := dal.Use(mysql.NewDB())
	if merchantID == "" {
		return "", errorx.New("商户id不能为空", -1)
	}
	if text == "" {
		return "", errorx.New("快捷回复内容不能为空", -1)
	}
	return dao.QuickReply{}.Add(ctx, query, merchantID, text)
}

// EditQuickReply  编辑快捷回复
func EditQuickReply(ctx context.Context, merchantID, id, text string) error {
	query := dal.Use(mysql.NewDB())
	exists := dao.QuickReply{}.Exists(ctx, query, id)
	if !exists {
		return errorx.New("快捷回复不存在", -1)
	}
	return dao.QuickReply{}.Edit(ctx, query, merchantID, id, text)
}

// DeleteQuickReply 删除快捷回复
func DeleteQuickReply(ctx context.Context, merchantID, id string) error {
	query := dal.Use(mysql.NewDB())
	exists := dao.QuickReply{}.Exists(ctx, query, id)
	if !exists {
		return errorx.New("快捷回复不存在", -1)
	}
	return dao.QuickReply{}.Delete(ctx, query, merchantID, id)
}

// QuickReplyList 快捷回复列表
func QuickReplyList(ctx context.Context, merchantID, text string) ([]*models.QuickReply, error) {
	if merchantID == "" {
		return []*models.QuickReply{}, errorx.New("商户id不能为空", -1)
	}
	query := dal.Use(mysql.NewDB())
	return dao.QuickReply{}.List(ctx, query, merchantID, text)
}
